package com.hainatong.shiphelper.module.cert;

import android.os.Bundle;
import android.view.View;
import com.base.common.utils.UIUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentCertDetailBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.QSHE.FileReaderActivity;
import com.hainatong.shiphelper.module.cert.entity.FileInfo;
import com.hainatong.shiphelper.module.cert.entity.ShipCertBean;
import com.hainatong.shiphelper.module.cert.viewmodel.CertViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.widget.LoadingDialog;


public class CertDetailFragment extends MvvmLazyFragment<FragmentCertDetailBinding, CertViewModel>
{
    private static String CERT_INFO_TAG = "CertDetailFragment";

    private ShipCertBean mCert;

    private LoadingDialog loading;

    public static CertDetailFragment newInstance(String scanCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( CERT_INFO_TAG , scanCode );
        CertDetailFragment certFragment = new CertDetailFragment();
        certFragment.setArguments( bundle );
        return certFragment;
    }


    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_cert_detail;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if(  args != null )
        {
           String certJsonStr = args.getString(CERT_INFO_TAG , "");
           if(UIUtils.isNotEmpty( certJsonStr ))
           {
               mCert = GsonUtil.json2Obj( certJsonStr, ShipCertBean.class );
           }
        }
    }


    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        loading = new LoadingDialog( getActivity(),getString( R.string.loading ) );


        // 查看证书
        viewDataBinding.rlCheckCert.setOnClickListener( v -> {

            if(mCert != null && !StringUtils.isEmpty( mCert.getFile_id() ))
            {
                loading.show();
                viewModel.queryFileInfoById( mCert.getFile_id() );
            }
        } );

        //申请 跟换
        viewDataBinding.btnApplyChange.setOnClickListener( v -> {

            Bundle bundle = new Bundle();
            bundle.putString(Constants.FRAGMNET_TYPE , Constants.VIEW_CHANGE_CERT);
            ComFragmentActivity.start(getActivity(),bundle);
        } );
    }


    @Override
    protected void lazyLoad()
    {
        //详情 和 mCert 一样
        /*
        viewModel.queryUserCertDetailById( mCert.getUuid() );
        viewModel.getCertDetailResult().observe( this , result -> {
        } );
        */

        if( UIUtils.isNotEmpty( mCert ) )
        {
            viewDataBinding.setCertBean( mCert );
            viewDataBinding.executePendingBindings();
        }
        // 查询 文件信息
        viewModel.getFileInfoResult().observe( this , result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                FileInfo theFile = result.getData();
                if(ObjectUtils.isNotEmpty( theFile ) && !StringUtils.isEmpty( theFile.getServerUrl() ) )
                {
                    FileReaderActivity.start(getActivity(),theFile.getServerUrl());
                    //ComFragmentActivity.startViewByParam(getActivity(), Constants.VIEW_DOCUMENT_VIEW, theFile.getServerUrl());
                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        loading.dismiss();
    }
}
